<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Utilisateur extends CI_Controller {

	public function index() {
		if($this->model->checkCookie()){
			$this->template->load('template', 'home_message'); //vue connecté
		}else{
			$this->template->load('template', 'welcome_message', array('page'=>'home', 'nomenu' => true)); //Vue connection
		}
	}

	public function home() {
		return $this->index();
	}

	public function deconnection(){
		$this->model->logoutUser();
		header('Location: '.base_url()); // redirection
	}
//formulaire inscription des participants
	public function inscription() {
		$data['groupes'] = $this->model->readAllGroup();
		$data['lvlClasse'] = $this->model->readSchoolAllLvl();
		$data['schls'] = $this->model->readSchool();
		$data['page'] = 'home';
		$this->template->load('template', 'ins_participant_message', $data);
	}

// affichage vue du gantt
	public function gantt() {
		$data['lesTaches'] = $this->model->ReadTache();
		$data['lesUsers'] = $this->model->readUsers();
		$data['lesItems'] = $this->model->readItem();
		$data['leGroupe'] = $this->model->readGroupUser(get_cookie('login_ndpi','value'));
		$this->template->load('template', 'gantt', $data);
	}

//affiche la vue valider utilisateur
	public function validation(){
		$data['utilisateurs'] = $this->model->readDisabledUser();
		$this->template->load('template', 'validation', $data);
	}
	
//Lire les tâches
	public function ReadTache() {
		$data['lesTaches'] = $this->model->ReadTache();
		$this->template->load('template', 'gantt', $data);
	}

//Créer les taches
	public function addTask() {
		$dateD = "2015-01-24 ".$this->input->post('TSK_start_hour_plan');
		$dateE = "2015-01-24 ".$this->input->post('TSK_end_hour_plan');

		$uneTache = array(
			'TSK_LIB' => $this->input->post('TSK_LIB'),
			'FK_USR' => $this->input->post('FK_USR'),
			'FK_ITM' => $this->input->post('FK_ITM'),
			'TSK_start_hour_plan' => $dateD,   
			'TSK_end_hour_plan' => $dateE
			);
		$this->model->CreateTache($uneTache);
    	header('Location: '.base_url().'utilisateur/gantt'); //redirection
    }

    public function modifTask($id) {
		$data['leGroupe'] = $this->model->readGroupUser(get_cookie('login_ndpi','value'));
		$data['lesUsers'] = $this->model->readUsers();
		$data['lesItems'] = $this->model->readItem();
    	$data['UneTache'] = $this->model->ReadOneTache($id);
    	$this->template->load('template', 'updateTache', $data);
    }

//Modifie les tâches
    public function UpdateTache() {
    	$user = $this->model->readUserByLibelle($this->input->post('FK_USR'));
    	$idUser = $user[0]->PK_USR;
    	$item = $this->model->readItemByLibelle($this->input->post('FK_ITM'));
    	$idItem = $item[0]->PK_ITM;

    	$uneTache = array(
			'TSK_LIB' => $this->input->post('TSK_LIB'),
			'FK_USR' => $idUser,
			'FK_ITM' => $idItem,
			'TSK_start_hour_plan' => $this->input->post('TSK_start_hour_plan'),   
			'TSK_end_hour_plan' => $this->input->post('TSK_end_hour_plan')
			);
    	$this->model->UpdateTache($uneTache);
    	
    	header('Location: '.base_url().'utilisateur/gantt'); //redirection
    }

//Supprime les tâches
    public function DeleteTache() {
    	$id = $this->uri->segment(3);
    	$this->model->DeleteTache($id);
    	$this->ReadTache();
    }

    //affiche la page todoList
    public function todoList(){
    	$data['todoList'] = $this->model->ReadTodo(get_cookie('login_ndpi',TRUE));
    	$this->template->load('template','todo_list',$data);
    }

    //suppression d'une tache du gantt
    public function deleteTask($id) {
    	$this->model->DeleteTache($id);
    	header('Location: '.base_url().'utilisateur/gantt'); //redirection
    }

}